//
//  Music_YDSlider.m
//  LEXY
//
//  Created by TKingLee on 14-9-7.
//  Copyright (c) 2014年 xyx. All rights reserved.
//

#import "Music_YDSlider.h"

@implementation Music_YDSlider

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    _slider.continuous = NO;
    //[self sendActionsForControlEvents:UIControlEventValueChanged];
    //[self sendActionsForControlEvents:UIControlEventTouchCancel];
    
    //[self addTarget:self action:@selector(handleValueChanged) forControlEvents:UIControlEventValueChanged];
    //[self addTarget:self action:@selector(handleMusicSliderDragExit) forControlEvents:UIControlEventTouchCancel];
    [_slider addTarget:self action:@selector(handleSliderTouched) forControlEvents:UIControlEventTouchDown];
    [_slider addTarget:self action:@selector(handleValueChanged) forControlEvents:UIControlEventValueChanged];
    [_slider addTarget:self action:@selector(handleMusicSliderDragExit) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark ---- Music Play
- (void)setAudioPath:(NSString *)audioPath{
    if (audioPath.length) {
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPath] error:nil];
        self.player.delegate = self;
        self.duration = self.player.duration;
        [self.player prepareToPlay];
    }
}




- (void)play{
    
    if (!self.player.playing) {
        if(self.timer.isValid)
        {
            [self.timer invalidate];
        }
        //alloc timer,interval:0.1 second
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateProgressCircle) userInfo:nil repeats:YES];
        [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
        [self.player play];
    }
}
- (void)pause{
    if (self.player.playing) {
        [self.timer invalidate];
        NSLog(@"%d",self.timer.isValid);
        [self.player pause];
    }
}

- (void)stop{
    //stop timer
    
    [self.player stop];
    self.player.currentTime = 0;
    [self updateProgressCircle];
}

- (void)updateProgressCircle{
    
    //update progress value
    float progress_ = (float) (self.player.currentTime / self.player.duration);
    
    self.value =  progress_;
    //redraw back & progress circles
    //[self setNeedsDisplay];
    
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(LineProgressViewDelegate)]) {
        [self.delegate updateProgressViewWithPlayer:self.player];
    }
}

- (void)handleSliderTouched{
    [self pause];
    self.player.currentTime = self.player.duration;
    [self updateProgressCircle];
    self.player.currentTime = self.value * self.player.duration;
}
- (void)handleValueChanged{
    //[self pause];
    
    //self.player.currentTime = self.player.duration;
    //[self updateProgressCircle];
    self.player.currentTime = self.value * self.player.duration;
    
    
    
    
    /*
     if (CGRectContainsPoint(self.bounds, point)) {
     [self pause];
     self.player.currentTime = self.player.duration;
     [self updateProgressCircle];
     if (recognizer.state == UIGestureRecognizerStateEnded) {
     [self play];
     self.playOrPauseButtonIsPlaying = YES;
     [self.delegate btnStateChange:NO];
     }
     }
     */
    
}

- (void)handleMusicSliderDragExit{
    /*
     [self play];
     */
    
    [self play];
    [self.delegate btnStateChange:NO];
    self.playOrPauseButtonIsPlaying = YES;
    
}

#pragma mark AVAudioPlayerDelegate method
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    if (flag) {
        //invalid timer
        [self.timer invalidate];
        //restore progress value
        self.progress = 0;
        //self redraw
        [self setNeedsDisplay];
        [self.delegate playerDidFinishPlaying];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
